package com.alogic.auth;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie管理器
 * 
 * @author yyduan
 * @version 1.6.12.11 [20181206 duanyy] <br>
 * - 浏览器cookie可定制httpOnly和secure属性 <br>
 *
 * @version 1.6.14.12 [20210617 duanyy] <br>
 * - cookie可支持SameSite属性 <br>
 */
public interface CookieManager {
	public static enum SameSite {
		NONE("None","__SAME_SITE_NONE__"),
		STRICT("Strict","__SAME_SITE_STRICT__"),
		LAX("Lax","__SAME_SITE_LAX__"),
		NULL("Null","");

		private String attributeValue;
		private String commentValue;

		private SameSite(String attributeValue,String commentValue) {
			this.attributeValue = attributeValue;
			this.commentValue = commentValue;
		}

		public String getAttributeValue() {
			return this.attributeValue;
		}

		public String getCommentValue(){
			return this.commentValue;
		}
	}
	/**
	 * 获取指定的Cookies值
	 * @param name Cookies名称
	 * @param dft 缺省值
	 * @return Cookies值
	 */
	public String getCookie(String name,String dft);	
	
	/**
	 * 设置Cookies
	 * @param name Cookies名称
	 * @param value 取值
	 */
	public void setCookie(String name,String value,String path,int ttl);
	
	/**
	 * 创建一个新的cookie
	 * @param name cookie名称
	 * @param value cookie取值
	 * @param path cookie的路径
	 * @return Cookie实例
	 */
	public Cookie newCookie(String name,String value,String path);
	
	/**
	 * 设置一个已经创建好的cookie
	 * @param cookie cookie实例
	 */
	public void setCookie(Cookie cookie);	
	
	/**
	 * 缺省实现
	 * @author yyduan
	 *
	 */
	public static class Default implements CookieManager{
		protected HttpServletRequest request = null;
		protected HttpServletResponse response = null;
		protected SessionManager sm = null;
		
		public Default(SessionManager sm,HttpServletRequest req,HttpServletResponse res){
			this.request = req;
			this.response = res;
			this.sm = sm;
		}

		@Override
		public String getCookie(String name, String dft) {
			return sm.getCookie(request, name, dft);
		}

		@Override
		public void setCookie(String name, String value, String path, int ttl) {
			sm.setCookie(response, name, value, path, ttl);
		}

		@Override
		public Cookie newCookie(String name, String value, String path) {
			return sm.newCookie(name, value, path);
		}

		@Override
		public void setCookie(Cookie cookie) {
			sm.setCookie(response, cookie);
		}
	}
}
